local zaowang = fk.CreateSkill{
  name = "rmt__zaowang",
  tags = { Skill.Limited }
}

zaowang:addEffect("active", {
  anim_type = "control",
  card_num = 0,
  target_num = 1,
  prompt = "#rmt__zaowang",
  can_use = function(self, player)
    return player:usedSkillTimes(zaowang.name, Player.HistoryGame) == 0
  end,
  card_filter = Util.FalseFunc,
  target_filter = function(self, player, to_select, selected)
    return #selected == 0
  end,
  on_use = function(self, room, effect)
    local player = effect.from
    local target = effect.tos[1]
    room:addPlayerMark(target, "@@rmt__zaowang", 1)
    if target.role == "rebel" then
      local tag = room.tag["rmt__zaowang_rebel"] or {}
      table.insertIfNeed(tag, target.id)
      room.tag["rmt__zaowang_rebel"] = tag
    end
    room:changeMaxHp(target, 1)
    if target:isWounded() and not target.dead then
      room:recover{
        who = target,
        num = 1,
        recoverBy = player,
        skillName = zaowang.name,
      }
    end
    if not target.dead then
      target:drawCards(3, zaowang.name)
    end
    room:setPlayerProperty(target, "role_shown", true)
    if target.role == "loyalist" then
      local lord = table.find(room.alive_players, function (p) return p.role == "lord" end)
      if lord then
        room:setPlayerProperty(lord, "role", "loyalist")
        room:setPlayerProperty(target, "role", "lord")
      end
    end
  end,
})

zaowang:addEffect(fk.GameOverJudge, {
  can_refresh = function(self, event, target, player, data)
    local tag = player.room.tag["rmt__zaowang_rebel"] or Util.DummyTable
    return target == player and table.contains(tag, player.id)
  end,
  on_refresh = function(self, event, target, player, data)
    local room = player.room
    room:gameOver("lord+loyalist")
  end,
})

Fk:loadTranslationTable{
  ["rmt__zaowang"] = "造王",
  [":rmt__zaowang"] = "限定技，出牌阶段，你可以令一名其他角色加1点体力上限、回复1点体力并摸三张牌，然后亮出其身份牌。若其为：忠臣，其与主公交换身份牌；反贼，当其死亡时，主公方获胜。",
  ["@@rmt__zaowang"] = "造王",
  ["#rmt__zaowang"] = "造王：令一名角色加1点体力上限、回复1点体力并摸三张牌！",
  ["$rmt__zaowang1"] = "大魏当兴，吾主可王。",
  ["$rmt__zaowang2"] = "身加九锡，当君不让。",
}

return zaowang
